package com.ruoyi.comparison.controller;

import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.comparison.domain.TaskCheck;
import com.ruoyi.comparison.domain.bo.TaskCheckBo;
import com.ruoyi.comparison.domain.vo.TaskCheckVo;
import com.ruoyi.comparison.service.ITaskCheckService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 查重任务
 *
 * @author ruoyi
 * @date 2023-11-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/check")
public class TaskCheckController extends BaseController {

    private final ITaskCheckService iTaskCheckService;

    /**
     * 查询查重任务列表
     */
    @SaCheckPermission("system:oss:list")
    @GetMapping("/list")
    public TableDataInfo<TaskCheckVo> list(TaskCheckBo bo, PageQuery pageQuery) {
        return iTaskCheckService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出查重任务列表
     */
    @SaCheckPermission("system:oss:export")
    @PostMapping("/export")
    public void export(TaskCheckBo bo, HttpServletResponse response) {
        List<TaskCheckVo> list = iTaskCheckService.queryList(bo);
        ExcelUtil.exportExcel(list, "查重任务", TaskCheckVo.class, response);
    }

    /**
     * 获取查重任务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:oss:query")
    @GetMapping("/{id}")
    public R<TaskCheckVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTaskCheckService.queryById(id));
    }

    /**
     * 获取userId
     */
    @SaCheckPermission("system:oss:query")
    @GetMapping("/getUserId")
    public R<String> getNowUserId(){
        String username = LoginHelper.getUsername();
        return R.ok(username);
    }
    /**
     * 新增查重任务
     */
    @SaCheckPermission("system:oss:add")
    @Log(title = "查重任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated @RequestBody TaskCheck taskCheck) {
        if (!iTaskCheckService.checkProjectNamUnique(taskCheck)) {
            return R.fail("新增任务'" + taskCheck.getProjectName() + "'失败，任务名称已存在");
        }
        return toAjax(iTaskCheckService.insertByBo(taskCheck));
    }

    /**
     * 开始查重任务
     */
    @SaCheckPermission("system:oss:list")
    @RepeatSubmit()
    @GetMapping("/start")
    public R<Void> startTask(Long id) throws Exception {
        //健壮性判断
        if (ObjectUtil.isEmpty(id)){
            throw new EmptyStackException();
        }
        iTaskCheckService.startTask(id);
        return R.ok();
    }

    /**
     * 修改查重任务
     */
    @SaCheckPermission("system:oss:edit")
    @Log(title = "查重任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaskCheckBo bo) {
        return toAjax(iTaskCheckService.updateByBo(bo));
    }

    /**
     * 删除查重任务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:oss:remove")
    @Log(title = "查重任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        Boolean b = iTaskCheckService.deleteWithValidByIds(Arrays.asList(ids));
        return toAjax(b);
    }
}
